﻿#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<Bitset>
#include"bitset.h"
#include"twoBitSet.h"
#include"bloomfilter.h"
using namespace std;
void test1()
{
	The_Song_of_the_end_of_the_world::BitSet<100> bs;
	bs.set(32);
	bs.set(33);
	bs.reset(34);
	cout << bs.test(32) << endl;
	cout << bs.test(33) << endl;
	cout << bs.test(34) << endl;
}
//解决问题
void test2()
{
	The_Song_of_the_end_of_the_world::BitSet<-1> bs;
	// 开2 ^ 32位的位图    
	//bit::bitset<-1> bs2;
	//bit::bitset<UINT_MAX> bs3;
	//bit::bitset<0xffffffff> bs4;
}
void test3()
{
	//会报错，因为STLbitset不是一个动态的数组而是静态的
	//std::bitset<-1> bs;
	// 建议VS2019，VS2022编译不通过
	//std::bitset<-1>* ptr=new std::bitset<-1>();
}
// 模拟位图找交集
void test_bitset1()
{
	int a1[] = { 5,7,9,2,5,99,5,5,7,5,3,9,2,55,1,5,6 };
	int a2[] = { 5,3,5,99,6,99,33,66 };
	bitset<100> bs1;
	bitset<100> bs2;
	for (auto e : a1)
	{
		bs1.set(e);
	}
	for (auto e : a2)
	{
		bs2.set(e);
	}
	for (size_t i = 0; i < 100; i++)
	{
		if (bs1.test(i) && bs2.test(i))
		{
			cout << i << endl;
		}
	}
}
void test_bitset2()
{
	The_Song_of_the_end_of_the_world::TwoBitSet<100> tbs;
	int a[] = { 5,7,9,2,5,99,5,5,7,5,3,9,2,55,1,5,6,6,6,6,7,9 };
	for (auto e : a)
	{
		tbs.set(e);
	}
	for (size_t i = 0; i < 100; ++i)
	{
		cout << i <<"->"<<tbs.get_count(i)<< " ";
	}
	cout<<endl;
	for (size_t i = 0; i < 100; ++i)
	{
		//cout << i << "->" << tbs.get_count(i) << endl;
		if (tbs.get_count(i) == 1 || tbs.get_count(i) == 2)
		{
			cout << i << " ";
		}
	}
	cout << endl;
}
void TestBloomFilter1()
{
	string strs[] = { "百度","字节","腾讯" };
	The_Song_of_the_end_of_the_world::BloomFilter<10> bf;
		for (auto& s : strs)
		{
				bf.Set(s);
		}
		for (auto& s : strs)
		{
				cout << bf.Test(s) << endl;
		}
		for (auto& s : strs)
		{
				cout << bf.Test(s + 'a') << endl;
		}
		cout << bf.Test("摆渡") << endl;
		cout << bf.Test("百渡") << endl;
}
void TestBloomFilter2() 
{
	srand(time(0));
	const size_t N = 10000000;
	BloomFilter<N> bf;
	//BloomFilter<N, 3> bf;
	//BloomFilter<N, 10> bf;
	std::vector<std::string> v1;
	std::string url = "https://www.cnblogs.com/clq / archive / 2012 / 05 / 31 / 2528153.html";
		////std::string url = "https://www.baidu.com/s?ie=utf
		//8 & f = 8 & rsv_bp = 1 & rsv_idx = 1 & tn = 65081411_1_oem_dg & wd = ln2 & fenlei = 256 & rsv_pq = 0x8d9962
		//630072789f & rsv_t = ceda1rulSdBxDLjBdX4484KaopD % 2BzBFgV1uZn4271RV0PonRFJm0i5xAJ % 2F
		//Do & rqlang = en & rsv_enter = 1 & rsv_dl = ib & rsv_sug3 = 3 & rsv_sug1 = 2 & rsv_sug7 = 100 & rsv_sug2 =
		//0 & rsv_btype = i & inputT = 330 & rsv_sug4 = 2535";
		////std::string url = "猪⼋戒";
		for (size_t i = 0; i < N; ++i)
		{
			v1.push_back(url + std::to_string(i));
		}
	for (auto& str : v1)
	{
		bf.Set(str);
	}
	// v2跟v1是相似字符串集（前缀⼀样），但是后缀不⼀样        v1.clear();
	for (size_t i = 0; i < N; ++i)
	{
		std::string urlstr = url;
		urlstr += std::to_string(9999999 + i);
		v1.push_back(urlstr);
	}
	size_t n2 = 0;
	for (auto& str : v1)
	{
		if (bf.Test(str)) // 误判
		{
			++n2;
		}
	}
	cout << "相似字符串误判率:" << (double)n2 / (double)N << endl;
		// 不相似字符串集前缀后缀都不⼀样        
		v1.clear();
}
int main()
{
	//test1();
	//test2();
	//test3();
	//test_bitset1();
	test_bitset2();
	return 0;
}



//// constructing bitsets
//#include <iostream>       // std::cout
//#include <string>         // std::string
//#include <bitset>         // std::bitset
//
//int main()
//{
//	std::bitset<-1>* ptr = new std::bitset<-1>();
//
//	return 0;
//}